#include "PhysicalLayer.h"
#include "ProtocolFactory.h"
#include <algorithm>


CPhysicalLayer::CPhysicalLayer(void)
{
	name = "PHYSICAL";
}

CPhysicalLayer::~CPhysicalLayer(void)
{
	layers.clear();
}

void CPhysicalLayer::addProtocol( const boost::shared_ptr<IProtocol>& protocol )
{
	add(protocol);
}

const boost::shared_ptr<IProtocol>& CPhysicalLayer::getProtocol( const std::string& name ) const
{
	citerator it = std::find_if(cbegin(), cend(), ProtocolComparer(name));

	if(it == cend()) 
	{
		throw new std::exception();
	}

	return *it;
}

void CPhysicalLayer::removeProtocol( const std::string& name )
{
	getContainerToModify().erase(std::remove_if(begin(), end(), ProtocolComparer(name)));
}

StackUtils::e_Status CPhysicalLayer::Decode( CPacket& packet )
{
	std::for_each(rbegin(), rend(), ProtocolDecode(packet));
	return StackUtils::E_SUCCESS;
}

StackUtils::e_Status CPhysicalLayer::Encode( CPacket& packet )
{
	std::for_each(begin(), end(), ProtocolEncode(packet));
	return StackUtils::E_SUCCESS;
}

void CPhysicalLayer::add( const boost::shared_ptr<IProtocol>& element )
{
	getContainerToModify().push_back(element);
}

void CPhysicalLayer::init()
{
	boost::shared_ptr<IProtocol> EP_ptr(CProtocolFactory::CreateProtocol("EP"));
	add(EP_ptr);
}

